package com.yanqu.road.server.cmd.sacrifice;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.sacrifice.SacrificeAttackBossResult;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.sacrifice.SacrificeProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.sacrifice.SacrificeMgr;
import com.yanqu.road.server.pb.SacrificePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_SACRIFICE_ATTACK_BOSS, desc = "攻击boss")
public class SacrificeAttackBossCmd extends AbstractCommand {

	private int patronsId;

	private SacrificeAttackBossResult result;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		SacrificeProto.SacrificeAttackBossReqMsg reqMsg = SacrificeProto.SacrificeAttackBossReqMsg.parseFrom(packet.getBytes());
		patronsId = reqMsg.getPatronsId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		result = SacrificeMgr.sacrificeAttackBoss(player, patronsId);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(null != result) {
			SacrificeProto.SacrificeAttackBossRespMsg.Builder respMsg = SacrificeProto.SacrificeAttackBossRespMsg.newBuilder();
			respMsg.setRet(result.getRet());
			if (0 == result.getRet()) {
				respMsg.setTotalBlood(result.getTotalBlood().toString());
				respMsg.setCurrentBlood(result.getCurrentBlood().toString());
				respMsg.setDamage(result.getDamage().toString());
				respMsg.setReward(result.getReward());
				if(!StringUtils.isNullOrEmpty(result.getKillBossNickName())) {
					respMsg.setUserData(SacrificePb.parseSacrificeKillBossUserInfoMsg(result.getKillBossNickName(), result.getKillBossDecoration()));
				}
				if(result.getKillBossUserId() > 0){
					setKillUserMsg(respMsg);
				}
			}else if(GameErrorCode.E_SACRIFICE_BOSS_IS_DEAD == result.getRet()){
				respMsg.setUserData(SacrificePb.parseSacrificeKillBossUserInfoMsg(result.getKillBossNickName(), result.getKillBossDecoration()));
				setKillUserMsg(respMsg);
			}
			player.sendPacket(Protocol.U_SACRIFICE_ATTACK_BOSS, respMsg);
		}
	}

	private void setKillUserMsg(SacrificeProto.SacrificeAttackBossRespMsg.Builder respMsg) {
		respMsg.setKillUserId(result.getKillBossUserId());
		UserInfo killUserInfo = UserMgr.getUserInfo(result.getKillBossUserId());
		UserBaseInfo killBaseInfo = UserMgr.parseUserBaseInfo(killUserInfo, GameServer.getInstance().getServerId());
		if (null != killBaseInfo) {
			PlayerProto.PlayerBaseTempMsg.Builder killUserMsg = PlayerBasePb.parsePlayerBaseTempMsg(killBaseInfo);
			respMsg.setKillUserInfo(killUserMsg);
		}
	}

}
